package com.bwie.system.controller;

import com.bwie.common.domain.User;
import com.bwie.common.result.Result;
import com.bwie.system.service.UserService;
import com.bwie.system.utils.EasyPoiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @className: UserController
 * @Description: TODO
 * @version: v1.８.0
 * @author:
 * @date:
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private EasyPoiUtil easyPoiUtil;

    @PostMapping("/findByTel")
    public Result<User> findByTel(@RequestBody User user){
        User byTel = userService.findByTel(user);
        return Result.success(byTel);
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("/uploadUsers")
    public Result uploadUsers(@RequestParam("file")MultipartFile file){
        List<User> list = easyPoiUtil.importFileExcel(file, User.class);

        Integer i = userService.addUserBatch(list);
        return Result.success();
    }

    @PostMapping("/putOutUsers")
    public void putOutUsers(@RequestBody User user, HttpServletResponse response){
        List<User> list = userService.userList(user);
        easyPoiUtil.exportExcel(response,list,User.class,"用户信息导出");
    }


}
